﻿// ---------------------------------------------------------------------
// Lớp Card_API
//
// Nội dung:
//    + Các hàm API xử lý lá bài 
//
// Copyright (c) 2009 YugiOH 
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611
// Võ Phước Hậu                 YM : phuochauspkt       ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServerYuGi;
using Common;
using Yugi.Client.Card;
using ServerYuGi.ChainEffectManager;
using ManagementEffect;
using ServerYuGi.PythonRegister;

namespace APIEffect.CARD
{
    public partial class CARD_API
    {
        SSupervisor super;
        public CARD_API(SSupervisor super)
        {
            this.super = super;
        }
        public void Card_SendTo_Graveyard(int ID)
        {
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            SCard tmp_Ban = player.LayCardTrenBan(ID);
            if (tmp_Ban != null)
            {
                DestroyCardOfPlayer(player, ID, tmp_Ban);
                super.ChainMnager.AddItemMultiChain(YCheckPoint.MONSTER_ENTER_GRAVEYARD, player.ID, tmp_Ban.MyYBaseCard.Card_TypeID, tmp_Ban.MyYBaseCard.ID
                                                    , "id_baixoa,is_byEffect", new object[] { tmp_Ban.MyYBaseCard.ID,true });
                return;
            }
            SPlayer Otherplayer = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            tmp_Ban = Otherplayer.LayCardTrenBan(ID);
            if (tmp_Ban != null)
            {
                DestroyCardOfPlayer(Otherplayer, ID, tmp_Ban);
                super.ChainMnager.AddItemMultiChain(YCheckPoint.MONSTER_ENTER_GRAVEYARD, Otherplayer.ID, tmp_Ban.MyYBaseCard.Card_TypeID, tmp_Ban.MyYBaseCard.ID
                                                    , "id_baixoa,is_byEffect", new object[] { tmp_Ban.MyYBaseCard.ID,true });
            }
        }
        public void Card_Remove_All_Script(int ID)
        {
            int idplayer = Card_Get_ID_Owner(ID);
            SPlayer player = super.LayPlayer(idplayer);
            SCard card = player.LayCardInAll(ID);
            super.EffectTurnManager.RemoveScript(card.MyYBaseCard.ID);
            super.pyregister.RemoveIDinStore(card);
        }
        public void Card_SendTo_OutZone(int id)
        {
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);           
            bool check =player.XoaBaiTrongMo(id);
            if(!check)
            {
                player = super.LayPlayerKhacVoiPlayerDangTrongLuot();
                player.MoveFromGraveyard2OutZone(id);
            }

            //super.SendMessage_CARD_XOABAI_GRAVEYARD_ToAll(player.ID, id);           
            
        }

        //-------------------------------Set-------------------------------//
        public void Card_SetDamage(int ID, int DiemCong)
        {
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            SCard tmp_Ban = player.LayCardTrenBan(ID);
            if (tmp_Ban != null)
            {
                SetCong(tmp_Ban, ID, DiemCong);
                return;
            }
            SPlayer Otherplayer = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            tmp_Ban = Otherplayer.LayCardTrenBan(ID);
            if (tmp_Ban != null)
                SetCong(tmp_Ban, ID, DiemCong);
        }

        public void Card_SetDefense(int ID, int DiemThu)
        {
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            SCard tmp_Ban = player.LayCardTrenBan(ID);
            if (tmp_Ban != null)
            {
                SetThu(tmp_Ban, ID, DiemThu);
                return;
            }
            SPlayer Otherplayer = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            tmp_Ban = Otherplayer.LayCardTrenBan(ID);
            if (tmp_Ban != null)
                SetThu(tmp_Ban, ID, DiemThu);
        }

        public void Summon_SetPosition(int ID, YMSG TrangThai)
        {
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            SCard tmp_Ban = player.LayCardTrenBan(ID);
            if (tmp_Ban != null)
            {
                SetTrangThai(tmp_Ban, ID, TrangThai);
                return;
            }
            SPlayer Otherplayer = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            tmp_Ban = Otherplayer.LayCardTrenBan(ID);
            if (tmp_Ban != null)
                SetTrangThai(tmp_Ban, ID, TrangThai);
        }

        //--------------------------------------Get---------------------------------------//
        public int Card_GetDamage(int ID)
        {            
            foreach (SPlayer player in super.Players)
            {
                foreach (SCard item in player.SoBaiTrenBan)
                {
                    if (item.MyYBaseCard.ID == ID)
                        return ((YMonsterCard)item.MyYBaseCard).Damage;
                }
            }
            return -1;
        }
        public int Card_GetDefense(int ID)
        {
            foreach (SPlayer player in super.Players)
            {
                foreach (SCard item in player.SoBaiTrenBan)
                {
                    if (item.MyYBaseCard.ID == ID)
                        return ((YMonsterCard)item.MyYBaseCard).Defend;
                }
            }
            return -1;
        }
        public YMSG Card_GetPosition(int ID)
        {
            foreach (SPlayer player in super.Players)
            {
                foreach (SCard item in player.SoBaiTrenBan)
                {
                    if (item.MyYBaseCard.ID == ID)
                        return item.TrangThai;
                }
            }
            return 0;
        }
        public string Card_Get_Card_Name(int ID)
        {
            foreach (SPlayer player in super.Players)
            {
                foreach (SCard item in player.SoBaiTrenBan)
                {
                    if (item.MyYBaseCard.ID == ID)
                        return item.MyYBaseCard.Name;
                }
            }
            return "";
        }
        //------------------------------------------Summon-------------------------
        
        public void Card_SpecialSummon_From_Hand(int player_id,int card_id)
        {
            SPlayer player= super.LayPlayer(player_id);
            if(player==null) return;
            SCard card= player.LayCardTrenTay(card_id);
            if (card == null) return;
            int vitrilabai = player.GetIndexCard_InHand(card_id);
            player.MoveFromHand2Table(card_id);
            player.Block.DuocPhep_TanCong.Add(card_id);
            super.SendMessage_API_SPECIALSUMMON_ToAll(player_id, card.MyYBaseCard.ID, card.MyYBaseCard.Card_TypeID, Zone.HAND);
            super.SendMessage_CARD_XOABAI_TAY_ToID(player_id, card_id, vitrilabai);
            super.SendMessage_TURN_MF1_BLOCK_ToID(player_id);            
        }
        public void Card_SpecialSummon_From_Deck(int player_id, int card_id)
        {
            SPlayer player = super.LayPlayer(player_id);
            if (player == null) return;
            SCard card = player.LayCardTrongBoBai(card_id);
            if (card == null) return;

            player.Block.DuocPhep_TanCong.Add(card_id);
            player.MoveFromDeck2Table(card_id);
            super.SendMessage_API_SPECIALSUMMON_ToAll(player_id, card.MyYBaseCard.ID, card.MyYBaseCard.Card_TypeID, Zone.DECK);
            super.SendMessage_TURN_MF1_BLOCK_ToID(player_id);

           
        }
        public void Card_SpecialSummon_From_Graveyard(int player_id, int card_id,int player_id_owner)
        {
            SPlayer player = super.LayPlayer(player_id);
            if (player == null) return;
            SCard card = player.LayCardTrongMo(card_id);
            if (card == null) return;            
            SPlayer playerowner = super.LayPlayer(player_id_owner);
            playerowner.SoBaiTrenBan.Add(card);
            player.SoBaiDaChet.Remove(card);

            playerowner.Block.DuocPhep_TanCong.Add(card.MyYBaseCard.ID);
            super.SendMessage_CARD_XOABAI_GRAVEYARD_ToAll(player_id, card_id);
            super.SendMessage_API_SPECIALSUMMON_ToAll(player_id_owner, card.MyYBaseCard.ID, card.MyYBaseCard.Card_TypeID, Zone.GRAVEYARD);
            super.SendMessage_TURN_MF1_BLOCK_ToID(player_id_owner);
                       
        }

        public YMonsterCard Card_ConvertFromBaseCard(YBaseCard card)
        {
            if(card is YMonsterCard)
                return (YMonsterCard)card;
            return null;
        }

        public YSpellCard Card_ConvertFromBaseCardToSpell(YBaseCard card)
        {
            if (card is YSpellCard)
                return (YSpellCard)card;
            return null;
        }

        public YTrapCard Card_ConvertFromBaseCardToTrap(YBaseCard card)
        {
            if (card is YTrapCard)
                return (YTrapCard)card;
            return null;
        }

        public int Card_Get_ID_Owner(int idcard)
        {
            SPlayer player = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            if (player.CheckIDInAll(idcard))
                return player.ID;
            player= super.LayPlayerKhacVoiID(player.ID);
            if (player.CheckIDInAll(idcard))
                return player.ID;
            return -1;
        }

        public void Card_Negate_Card_Before(int idCardActive)
        {
            super.NegateCardBefore(idCardActive);
        }

        public YBaseCard Get_Type_Last_Chain()
        {
            try
            {
                ItemChain chain = super.ChainMnager.GetLastItem();
                int IDPlayer = Card_Get_ID_Owner(chain.ID);
                return super.LayPlayer(IDPlayer).LayCardTrenBan(chain.ID).MyYBaseCard;
            }
            catch (System.Exception e)
            {
                return null;
            }
            
             
        }
        public void Card_Move_From_Deck_To_Hand(int player_id, int card_id)
        {
            SPlayer player = super.LayPlayer(player_id);
            if (player == null) return;
            SCard card = player.LayCardTrongBoBai(card_id);
            if (card == null) return;            
            player.MoveFromDeck2Hand(card.MyYBaseCard.ID);
            super.SendMessage_API_MOVECARD_ToID(player.ID, card.MyYBaseCard.Card_TypeID, card.MyYBaseCard.ID, Zone.DECK, Zone.HAND);

            SPlayer OtherPlayer = super.LayPlayerKhacVoiID(player.ID);
            byte[] data = new byte[] {0};
            super.SendMessage_API_MOVECARD_DOITHU_ToID(OtherPlayer.ID, Zone.DECK, Zone.HAND,data);
        }

        public void Card_Move_From_Table_To_Hand(int player_id, int card_id)
        {
            SPlayer player = super.LayPlayer(player_id);
            if (player == null) return;
            SCard card = player.LayCardTrenBan(card_id);
            if (card == null) return;            
            player.MoveFromTable2Hand(card.MyYBaseCard.ID);
            super.SendMessage_API_MOVECARD_ToID(player.ID, card.MyYBaseCard.Card_TypeID, card.MyYBaseCard.ID, Zone.TABLE, Zone.HAND);
            SPlayer OtherPlayer = super.LayPlayerKhacVoiID(player.ID);
            //byte[] data = new byte[] { 1,(byte)card.MyYBaseCard.Card_TypeID,(byte)card.MyYBaseCard.ID }; // old
            List<byte> dataSend = new List<byte>();
            dataSend.Add(1);
            dataSend.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
            dataSend.Add((byte)card.MyYBaseCard.ID);
            super.SendMessage_API_MOVECARD_DOITHU_ToID(OtherPlayer.ID, Zone.TABLE, Zone.HAND, dataSend.ToArray());
        }

        public void Card_Move_From_Table_To_Deck(int player_id, int card_id)
        {
            SPlayer player = super.LayPlayer(player_id);
            if (player == null) return;
            SCard card = player.LayCardTrenBan(card_id);
            if (card == null) return;
            player.MoveFromTable2Deck(card.MyYBaseCard.ID);
            super.SendMessage_API_MOVECARD_ToID(player.ID, card.MyYBaseCard.Card_TypeID, card.MyYBaseCard.ID, Zone.TABLE, Zone.DECK);
            SPlayer OtherPlayer = super.LayPlayerKhacVoiID(player.ID);
            //byte[] data = new byte[] { 1, (byte)card.MyYBaseCard.Card_TypeID, (byte)card.MyYBaseCard.ID }; // old

            List<byte> dataSend = new List<byte>();
            dataSend.Add(1);
            dataSend.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
            dataSend.Add((byte)card.MyYBaseCard.ID);

            super.SendMessage_API_MOVECARD_DOITHU_ToID(OtherPlayer.ID, Zone.TABLE, Zone.DECK, dataSend.ToArray());
        }

        public void Card_Set_Damage_Calculation(int ID,int DiemCong)
        {
            super.flagManager.SetValue("isChangeDamage",true);
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            SCard tmp_Ban = player.LayCardTrenBan(ID);
            if (tmp_Ban != null)
            {
                SetCong(tmp_Ban, ID, DiemCong);
                return;
            }
            SPlayer Otherplayer = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            tmp_Ban = Otherplayer.LayCardTrenBan(ID);
            if (tmp_Ban != null)
                SetCong(tmp_Ban, ID, DiemCong);
        }



        #region private void
        void SetCong(SCard tmp_Ban,int ID, int DiemCong)
        {
            YMonsterCard card = (YMonsterCard)tmp_Ban.MyYBaseCard;
            card.Damage = DiemCong;
            tmp_Ban.MyYBaseCard = card;
            List<byte> listbyte = new List<byte>();
            listbyte.Add((byte)ID);
            listbyte.AddRange(BitConverter.GetBytes(DiemCong));           
            super.SendMessage_CARD_SETCONG_ToAll(listbyte.ToArray());
        }

        void SetThu(SCard tmp_Ban, int ID, int DiemThu)
        {
            YMonsterCard card = (YMonsterCard)tmp_Ban.MyYBaseCard;
            card.Defend = DiemThu;
            tmp_Ban.MyYBaseCard = card;
            List<byte> listbyte = new List<byte>();
            listbyte.Add((byte)ID);
            listbyte.AddRange(BitConverter.GetBytes(DiemThu));            
            super.SendMessage_CARD_SETTHU_ToAll(listbyte.ToArray());
        }
        void SetTrangThai(SCard tmp_Ban, int ID, YMSG TrangThai)
        {
            tmp_Ban.TrangThai = TrangThai;
            SPlayer playerowner = super.LayPlayer(this.Card_Get_ID_Owner(ID));
            playerowner.Block.DuocPhep_DoiTrangThai.Remove(tmp_Ban.MyYBaseCard.ID);
            super.SendMessage_CARD_DOITRANGTHAI_XONG_ToAll(ID, TrangThai);
        }

        void DestroyCardOfPlayer(SPlayer player, int ID,SCard tmp_Ban)
        {            
            super.SendMessage_CARD_XOABAI_ToAll(ID);
            player.CheckXoaScript(super,tmp_Ban);
            player.XoaLaBaiHySinh(tmp_Ban);
        }
        

        #endregion
    }
}
